package com.alatus.service.impl;

import com.alatus.constant.Constants;
import com.alatus.mapper.TActivityRemarkMapper;
import com.alatus.model.TActivityRemark;
import com.alatus.query.ActivityRemarkQuery;
import com.alatus.service.ActivityRemarkService;
import com.alatus.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {
    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setCreateBy(JWTUtils.parseUserFromJWT(activityRemarkQuery.getToken()).getId());
        tActivityRemark.setCreateTime(new Date());
        BeanUtils.copyProperties(activityRemarkQuery,tActivityRemark);
        return tActivityRemarkMapper.insert(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityRemarkByPage(Integer current,ActivityRemarkQuery activityRemarkQuery) {
//        设置PageHelper和分页情况
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<TActivityRemark> list = tActivityRemarkMapper.selectActivityRemarkByPage(activityRemarkQuery);
        PageInfo<TActivityRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();
        BeanUtils.copyProperties(activityRemarkQuery,tActivityRemark);
        tActivityRemark.setEditTime(new Date());
        tActivityRemark.setEditBy(JWTUtils.parseUserFromJWT(activityRemarkQuery.getToken()).getId());
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteActivtyRemarkById(Integer id) {
//        修改删除状态法,可恢复
//        TActivityRemark tActivityRemark = new TActivityRemark();
//        tActivityRemark.setId(id);
//        tActivityRemark.setDeleted(1);
//        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
        return tActivityRemarkMapper.deleteByPrimaryKey(id);
    }
//    @Override
//    public TActivityRemark getActivityRemarkById(Integer id) {
//        return tActivityRemarkMapper.selectByPrimaryKey(id);
//    }
}
